package grape.base.rest.message.mvc;

import com.baomidou.mybatisplus.core.metadata.IPage;
import grape.base.rest.message.form.MessageSendForm;
import grape.base.service.message.query.MessageListQuery;
import grape.common.exception.runtime.BadRequestException;
import grape.common.exception.runtime.RDataNotExistException;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import javax.validation.Valid;
import grape.base.rest.message.form.MessageCreateForm;
import grape.base.rest.message.form.MessageUpdateForm;
import grape.base.rest.message.form.MessageListPageForm;
import grape.base.rest.message.vo.MessageVo;
import grape.base.rest.message.mapper.MessageWebMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;
import grape.common.rest.mvc.BaseController;
import grape.base.service.message.po.Message;
import grape.base.service.message.api.IMessageService;
import java.util.List;
/**
 * <p>
 * 消息表 前端控制器
 * </p>
 *
 * @author yangwei
 * @since 2020-03-18
 */
@RestController
@RequestMapping("/base/message")
@Api(tags = "消息相关接口")
public class MessageController extends BaseController<MessageVo, Message> {

    @Autowired
    private MessageWebMapper currentWebMapper;
    @Autowired
    private IMessageService currentService;



     @ApiOperation("添加消息")
     @PreAuthorize("hasAuthority('message:single:create')")
     @PostMapping
     @ResponseStatus(HttpStatus.CREATED)
     public MessageVo create(@RequestBody @Valid MessageCreateForm cf) {
         Message po = currentWebMapper.formToPo(cf);
         po.setIsSend(false);
         return super.create(po);
     }

     @ApiOperation("根据ID查询消息")
     @PreAuthorize("hasAuthority('message:single:queryById')")
     @GetMapping("/{id}")
     @ResponseStatus(HttpStatus.OK)
     public MessageVo queryById(@PathVariable String id) {
         return super.queryById(id);
     }

     @ApiOperation("删除消息")
     @PreAuthorize("hasAuthority('message:single:deleteById')")
     @DeleteMapping("/{id}")
     @ResponseStatus(HttpStatus.NO_CONTENT)
     public boolean deleteById(@PathVariable String id) {
         return super.deleteById(id);
     }

     @ApiOperation("更新消息")
     @PreAuthorize("hasAuthority('message:single:updateById')")
     @PutMapping("/{id}")
     @ResponseStatus(HttpStatus.CREATED)
     public MessageVo update(@PathVariable String id,@RequestBody @Valid MessageUpdateForm uf) {
         Message po = currentWebMapper.formToPo(uf);
         po.setId(id);
         // 如果消息已发送不能编辑
         Message message = currentService.getById(id);
         if (message.getIsSend()) {
             throw new BadRequestException("该消息已发送，不能编辑");
         }
         return super.update(po);
     }

    @ApiOperation("分页查询消息")
    @PreAuthorize("hasAuthority('message:single:listPage')")
    @GetMapping("/listPage")
    @ResponseStatus(HttpStatus.OK)
    public IPage<MessageVo> listPage(MessageListPageForm listPageForm) {
         Message po = currentWebMapper.formToPo(listPageForm);
        MessageListQuery messageListQuery = new MessageListQuery();
        messageListQuery.setPoQuery(po);
        return super.listPage(messageListQuery,listPageForm);
     }
    @ApiOperation("群发消息")
    @PreAuthorize("hasAuthority('message:single:sendall')")
    @PostMapping("/{messageId}/sendall")
    @ResponseStatus(HttpStatus.CREATED)
    public Boolean sendAll(@RequestBody @Valid MessageSendForm cf,@PathVariable String messageId ) {
        Message po = currentWebMapper.formToPo(cf);
        po.setId(messageId);
        Message one = currentService.getOne(po);
        if (one == null) {
            throw new RDataNotExistException("数据不存在，请刷新后再试");
        }
        if(one.getIsSend()){
            throw new BadRequestException("该消息已发送，不能重复发送");
        }
        // todo 消息发送
        one.setIsSend(true);
        currentService.updateById(one);
        return true;
    }
}
